#include "lw_log.h"
#include "elog.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>

// 全局日志配置
lw_log_config_t g_log_config = {
    .level = LW_LOG_INFO,
    .output = NULL,
    .enable_timestamp = 1,
    .enable_color = 0
};

// ANSI颜色代码
#define COLOR_RED     "\033[31m"
#define COLOR_YELLOW  "\033[33m"
#define COLOR_GREEN   "\033[32m"
#define COLOR_BLUE    "\033[34m"
#define COLOR_RESET   "\033[0m"

// 级别名称
static const char* level_names[] = {
    "NONE",
    "ERROR",
    "WARN",
    "INFO",
    "DEBUG"
};

// 级别颜色
static const char* level_colors[] = {
    "",
    COLOR_RED,
    COLOR_YELLOW,
    COLOR_GREEN,
    COLOR_BLUE
};

void lw_log_init(lw_log_level_t level, FILE* output, int enable_timestamp, int enable_color) {
    // 初始化EasyLogger
    elog_init();
    
    // 设置日志格式
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL);
    
    // 启动EasyLogger
    elog_start();
    
    // 设置兼容性配置
    g_log_config.level = level;
    g_log_config.output = output ? output : stdout;
    g_log_config.enable_timestamp = enable_timestamp;
    g_log_config.enable_color = enable_color;
    
    // 根据配置设置EasyLogger级别
    switch(level) {
        case LW_LOG_ERROR:
            elog_set_filter_lvl(ELOG_LVL_ERROR);
            break;
        case LW_LOG_WARN:
            elog_set_filter_lvl(ELOG_LVL_WARN);
            break;
        case LW_LOG_INFO:
            elog_set_filter_lvl(ELOG_LVL_INFO);
            break;
        case LW_LOG_DEBUG:
            elog_set_filter_lvl(ELOG_LVL_DEBUG);
            break;
        default:
            elog_set_filter_lvl(ELOG_LVL_INFO);
            break;
    }
}

void lw_log_set_level(lw_log_level_t level) {
    g_log_config.level = level;
    
    // 同步设置EasyLogger级别
    switch(level) {
        case LW_LOG_ERROR:
            elog_set_filter_lvl(ELOG_LVL_ERROR);
            break;
        case LW_LOG_WARN:
            elog_set_filter_lvl(ELOG_LVL_WARN);
            break;
        case LW_LOG_INFO:
            elog_set_filter_lvl(ELOG_LVL_INFO);
            break;
        case LW_LOG_DEBUG:
            elog_set_filter_lvl(ELOG_LVL_DEBUG);
            break;
        default:
            elog_set_filter_lvl(ELOG_LVL_INFO);
            break;
    }
}

void lw_log(lw_log_level_t level, const char* tag, const char* format, ...) {
    // 检查日志级别
    if (level > g_log_config.level || level <= LW_LOG_NONE) {
        return;
    }
    
    // 使用EasyLogger输出日志
    switch(level) {
        case LW_LOG_ERROR:
            elog_error(tag, format);
            break;
        case LW_LOG_WARN:
            elog_warn(tag, format);
            break;
        case LW_LOG_INFO:
            elog_info(tag, format);
            break;
        case LW_LOG_DEBUG:
            elog_debug(tag, format);
            break;
        default:
            elog_info(tag, format);
            break;
    }
}